package com.datastructure.test;

import com.datastructure.test.doubletoken.lengthofnorepeatstring.self.MaxLengthOfNoRepeatingString;
import com.datastructure.test.maxlengthofchildserial.MaxLengthOfChildSerial;

import java.util.HashMap;
import java.util.Map;

public class MaxLengthOfNoRepeatingSerial {

    public static void main(String[] args) {

    }

    public String getMaxLengthOfNoRepeatingSerial(String s){
        Map<Character,Integer> map = new HashMap<>();
        int maxLength = 0;
        int indexI = 0;
        for (int i = 0,j=0; i < s.length(); i++) {
            if(map.containsKey(s.charAt(i))){
                if((i-j+1)>maxLength){
                   indexI=i;
                }
                j = Math.max(j,map.get(s.charAt(i))+1);
            }
            map.put(s.charAt(i),i);
            maxLength = Math.max(maxLength,i-j+1);
        }
        return s.substring(indexI,maxLength);
    }
}
